﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using EnCor.ObjectBuilder;

namespace EnCor.Logging
{
    [AssembleConfig(typeof(FileLogAppenderConfig))]
    public class FileLogAppender : LogAppender
    {
        public string _FilePath;

        public FileLogAppender(string filePath)
        {
            if (!File.Exists(filePath))
            {
                File.Create(filePath).Close();
            }
            _FilePath = filePath;
        }

        public override void Log(LogEntry logEntry)
        {
            using (FileStream fs = new FileStream(_FilePath, FileMode.Append, FileAccess.Write))
            {
                StreamWriter sw = new StreamWriter(fs);

                sw.WriteLine(string.Format("date:{0} \r\nthread:{1} \r\nloglevel:{2} \r\nlogger:{3} \r\nmessage:{4}", 
                    logEntry.TimeStamp, 
                    logEntry.ThreadName,
                    logEntry.Level, 
                    logEntry.LoggerName, 
                    logEntry.Message));
                sw.Flush();
            }
        }
    }
}
